package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketSearchHistory;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.CategoryVO;
import com.cskaoyan.bean.vo.wx.GoodDetailVO;
import com.cskaoyan.bean.vo.wx.ListCategoryVO;
import com.cskaoyan.service.admin.GoodsService;
import com.cskaoyan.service.admin.MarketCategoryService;
import com.cskaoyan.service.wx.MarketSearchHistoryService;
import com.cskaoyan.validator.LoginUser;
import com.cskaoyan.validator.Order;
import com.cskaoyan.validator.Sort;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author klw
 * @Data 2022/11/22 下午5:21
 */

@RestController
@RequestMapping("wx/goods")
public class WxGoodController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    MarketCategoryService marketCategoryService;

    @Autowired
    MarketSearchHistoryService searchHistoryService;

    @GetMapping("count")
    public BaseRespVo count(){
        int totalGoods = goodsService.selectCount();
        return BaseRespVo.ok(totalGoods);
    }

    @GetMapping("category")
    public BaseRespVo categoryDetail(Integer id){
        CategoryVO categoryVO = marketCategoryService.selectCategoryDetail(id);
        return BaseRespVo.ok(categoryVO);
    }

//    @GetMapping("list")  // 翻页需求 ? 到底显示几个?
//    public BaseRespVo list(String categoryId, int page, int limit){
//        ListCategoryVO listCategoryVO = marketCategoryService.getListCategoryVO(categoryId,page,limit);
//        return BaseRespVo.ok(listCategoryVO);
//    }

    @GetMapping("list")
    public Object list(
            Integer categoryId,
            Integer brandId,
            String keyword,
            Boolean isNew,
            Boolean isHot,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @Sort(accepts = {"add_time", "retail_price", "name"}) @RequestParam(defaultValue = "add_time") String sort,
            @Order @RequestParam(defaultValue = "desc") String order) {

        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipal();
        Integer userId = null;
        if(null == user){
            userId = null; // 没登录
        }else {
            userId =  user.getId();
        }

        //添加到搜索历史
        if (userId != null && !StringUtils.isEmpty(keyword)) {
            MarketSearchHistory searchHistoryVo = new MarketSearchHistory();
            searchHistoryVo.setKeyword(keyword);
            searchHistoryVo.setUserId(userId);
            searchHistoryVo.setFrom("wx");
            searchHistoryService.save(searchHistoryVo);
        }

        //查询列表数据
        List<MarketGoods> goodsList = goodsService.querySelective(categoryId, brandId, keyword, isHot, isNew, page, limit, sort, order);

        // 查询商品所属类目列表。
        List<Integer> goodsCatIds = goodsService.getCatIds(brandId, keyword, isHot, isNew);
        List<MarketCategory> categoryList = null;
        if (goodsCatIds.size() != 0) {
            categoryList = marketCategoryService.queryL2ByIds(goodsCatIds);
        } else {
            categoryList = new ArrayList<>(0);
        }

        PageInfo<MarketGoods> pagedList = PageInfo.of(goodsList);

        Map<String, Object> entity = new HashMap<>();
        entity.put("list", goodsList);
        entity.put("total", pagedList.getTotal());
        entity.put("page", pagedList.getPageNum());
        entity.put("limit", pagedList.getPageSize());
        entity.put("pages", pagedList.getPages());
        entity.put("filterCategoryList", categoryList);

        // 因为这里需要返回额外的filterCategoryList参数，因此不能方便使用ResponseUtil.okList
        return BaseRespVo.ok(entity);
    }

    @GetMapping("detail")
    public BaseRespVo getGoodDetail(Integer id){
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipal();
        Integer userId = 0;
        if(null == user){
            userId = -1; // 没登录 --> 没收藏
        }else {
            userId =  user.getId();
        }
        //Integer userId = 1;
        GoodDetailVO goodDetail = goodsService.getGoodDetail(id, userId);
        return BaseRespVo.ok(goodDetail);
    }

    @GetMapping("related")
    public BaseRespVo <CommonData> getRelatedGoods(Integer id){
        CommonData <MarketGoods> res = goodsService.getRelatedGoods(id);
        return BaseRespVo.ok(res);
    }
}
